define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	var STATUS = pubjs.config('TASK_STATUS');
	var TYPE = pubjs.config('TASK_TYPE');

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				urlRemove: 'sync/remove'
				,urlReset: 'sync/reset'
			});

			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			this.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('任务列表')+'</span>',
				'</div>',
				'<div class="" pub-name="grid" pub-mod="exchange/grid/task.base"/>'
			].join('')));


			this.buildFromTemplate({
				grid: {
					'hasFilter': true
				}
			});

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'height': 400,
				'items': [
					{'name': 'adxId', 'uri':'modules/sideBarBase.channel','config':{'monitorUrl':''}},
					{'name': 'Status', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('任务状态'),
						'data': [
							{id: 0, name: LANG('全部')},
							{id: STATUS.DONE, name: LANG('已完成')},
							{id: STATUS.FAIL, name: LANG('失败')},
							{id: STATUS.ING, name: LANG('进行中')},
							{id: STATUS.WAIT, name: LANG('等待中')}
						],
						'value': 0
					}},
					{'name': 'Type', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('类型'),
						'data': [
							{id: 0, name: LANG('全部')},
							{id: TYPE.SPOT_SYNC, name: LANG('广告位同步')},
							{id: TYPE.DEAL_SYNC, name: LANG('订单广告位同步')},
						],
						'value': 0
					}}
				]
			});
		}
		,onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'Status':
						param.Status = +id;
						break;
					case 'Type':
						param.Type = +id;
						break;
					case 'adxId':
						param.AdxId = +id;
						break;
				}
			});

			self.$.grid.setParam(param).load();
		}
		,onInputClick: function(ev) {
			return false;
		}
		,reload: function() {
			this.$.grid.load();
		}
		,reset: function() {
			return this;
		}
		,onOperateMenuShow: function(ev){
			var param = ev.param;
			var result = [];

			if([STATUS.DONE, STATUS.FAIL].indexOf(param.Status) > -1)
			{
				result.push({id: 'reset', name: LANG('重跑')});
			}

			if(!result.length)
			{
				result.push({id: '', name: '没有操作'});
			}

			ev.returnValue = result;
			return false;
		}
		// 操作菜单-响应菜单操作
		,onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];
			var c = this.getConfig();
			var id = +data._id;
			var self = this;

			var url = '';
			var param = {
				_id: id
			};

			switch(op.key){
				case 'reset':
					url = c.urlReset;
					break;
				case 'remove':
					url = c.urlRemove;
					break;
				default :
					return;
			}


			pubjs.loading.show();
			pubjs.mc.send(url, param, function(err, data) {
				pubjs.loading.hide();
				if(err)
				{
					return pubjs.alert(err.message);
				}

				pubjs.alert('操作成功');
				self.$.grid.reload();
			});

			return false;
		}
	});

	exports.list = List;
});